package gui;

import java.awt.*;

import javax.swing.*;
import java.util.*;
import weerapp.meet.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.*;
import org.jfree.data.xy.*;

@SuppressWarnings("serial")
public class GrafiekPanel extends JPanel {

	private JFreeChart freechart;

	private ArrayList<Waarneming> waarnemingen;

	private XYSeries minTemp;

	private XYSeries maxTemp;

	public GrafiekPanel(ArrayList<Waarneming> waarnemingen) {
		setLayout(new BorderLayout());
		this.waarnemingen = waarnemingen;
		minTemp = new XYSeries("min Temp", true, false);
		maxTemp = new XYSeries("max Temp", true, false);
		DefaultTableXYDataset dataset = new DefaultTableXYDataset();
		minimumTemp();
		maximumTemp();
		dataset.addSeries(maxTemp);
		dataset.addSeries(minTemp);
		
		freechart = ChartFactory.createXYLineChart("Temperaturen", "dag", "°C", dataset, 
				PlotOrientation.VERTICAL, true, true, false);
		
		
		int maand = (waarnemingen.size()>0) ? getAantalDagenVanMaand(waarnemingen.get(0).getMaand()):0;
		
		XYPlot plot = freechart.getXYPlot();				
		plot.getDomainAxis().setRange(1, maand);
		NumberAxis axis = (NumberAxis)plot.getRangeAxis();
		axis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		axis = (NumberAxis)plot.getDomainAxis();
		axis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		
		ChartPanel chartpanel = new ChartPanel(freechart);
		chartpanel.setLayout(new BorderLayout());
		add(chartpanel, BorderLayout.CENTER);
	}

	public void minimumTemp() {
		for(Waarneming w: waarnemingen){
			minTemp.add((int)w.getDag(), w.getMinTemp());			
		}
	}
	
	public void maximumTemp(){
		for(Waarneming w: waarnemingen){
			maxTemp.add((int)w.getDag(), w.getMaxTemp());
		}
	}
	
	private int getAantalDagenVanMaand(int maand)
	{
		switch(maand){
		case 1: return 31;
		case 2: return 28;
		case 3: return 31;
		case 4: return 30;
		case 5: return 31;
		case 6: return 30;
		case 7: return 31;
		case 8: return 31;
		case 9: return 30;
		case 10:return 31;
		case 11:return 30;
		case 12:return 31;
		default : return 31;
		}
		
	}
	
	

}
